package com.example.forum.controller;

import com.example.forum.common.AppConfig;
import com.example.forum.common.AppResult;
import com.example.forum.common.ResultCode;
import com.example.forum.exception.ApplicationException;
import com.example.forum.model.Message;
import com.example.forum.model.User;
import com.example.forum.services.IMessageService;
import com.example.forum.services.IUserService;
import io.swagger.models.auth.In;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Slf4j
@RequestMapping("/message")
public class MessageController {

    @Autowired
    private IUserService iUserService;
    @Autowired
    private IMessageService iMessageService;
    @PostMapping("/send")
    public AppResult send(HttpServletRequest request ,
                          @NonNull Long receiveUserId,
                          @NonNull String content){
        HttpSession session = request.getSession(false);
        //校验发送者的信息
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if(user.getState()!=0){
            log.warn(ResultCode.FAILED_USER_BANNED.toString());
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }

        //不能给自己发送
        if(user.getId()==receiveUserId){
            log.warn("不能给自己发送");
            return  AppResult.failed(ResultCode.FAILED_CREATE);
        }

        //查询接受用户的个人信息
        User user1 = iUserService.SelectById(receiveUserId);
        if(user1==null||user1.getDeleteState()==1){
            log.warn("接收用户不存在");
            return AppResult.failed(ResultCode.FAILED_USER_NOT_EXISTS);

        }

        //构造对象
        Message message = new Message();
        message.setPostUserId(user.getId());
        message.setReceiveUserId(receiveUserId);
        message.setContent(content);
        // 调⽤业务层
        iMessageService.create(message);
        // 返回结果
        return AppResult.success();
    }

    @GetMapping("/getUnreadCount")
    public AppResult<Integer> getUnreadCount(HttpServletRequest request){
        // 获取发送⽤⼾信息
        HttpSession session = request.getSession();
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        // 查询未读消息个数
        Integer result = iMessageService.selectUnreadCount(user.getId());
        // 返回结果
        return AppResult.success(result);
    }


    @GetMapping("/getAll")
    public AppResult<List<Message>> getAll(HttpServletRequest request){
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        List<Message> messages = iMessageService.selectByReceiveUserId(user.getId());
        return AppResult.success(messages);

    }

    @PostMapping("/markRead")
    public AppResult markRead(HttpServletRequest request,@NonNull Long id){
        //获取当前登录用户的信息
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);


        //查询消息的内容
        Message message = iMessageService.selectById(id);
        //查询接收放是不是当前登录的用户
        if(user.getId()!= message.getReceiveUserId()){
            return AppResult.failed(ResultCode.FAILED_UNAUTHORIZED);
        }

        //更新为已读状态
        iMessageService.updateStateById(message.getId(), (byte) 1);
        return AppResult.success();

    }

    @PostMapping("/reply")
    public AppResult reply(HttpServletRequest request, @NonNull Long repliedId,@NonNull String content){
        //校验当前用户的状态
        HttpSession session = request.getSession(false);
        User user = (User) session.getAttribute(AppConfig.USER_SESSION);
        if (user.getState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_USER_BANNED);
        }
        //校验要回复的站内信息
        Message existsMessage = iMessageService.selectById(repliedId);
        if (existsMessage == null || existsMessage.getDeleteState() == 1) {
            // 返回错误描述
            return AppResult.failed(ResultCode.FAILED_NOT_EXISTS);
        }
        //不能给自己回复
        if(user.getId()==existsMessage.getPostUserId()){
            return AppResult.failed("不能给自己回复消息");
        }

        //构造对象
        Message message1 = new Message();
        message1.setPostUserId(user.getId());
        message1.setReceiveUserId(existsMessage.getPostUserId());
        message1.setContent(content);

        iMessageService.reply(repliedId,message1);
        return AppResult.success();


    }

}
